作者:闹剧-豆腐渣_141 | 来源:互联网 | 2023-08-15 04:21
我知道文档显示了如何列出这样的项目:
listRef.listAll()
.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(ListResult listResult) {
for (StorageReference prefix : listResult.getPrefixes()) {
// All the prefixes under listRef.
// You may call listAll() recursively on them.
}
for (StorageReference item : listResult.getItems()) {
// All the items under listRef.
}
}
})
...但是如何从item
中获取数据?例如,图像uri或元数据。
public class Wallpaper {
private String wallpaperName;
private Uri imageUri;
public Wallpaper(String wallpaperName,Uri imageUri) {
this.wallpaperName = wallpaperName;
this.imageUri = imageUri;
}
public Wallpaper() {
}
public String getWallpaperName() {
return wallpaperName;
}
public void setWallpaperName(String wallpaperName) {
this.wallpaperName = wallpaperName;
}
public Uri getImageUri() {
return imageUri;
}
public void setImageUri(Uri imageUri) {
this.imageUri = imageUri;
}
}
public class ShowWallpapers extends AppCompatactivity {
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
private List wallpaperList;
private StorageReference storageReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_wallpapers);
storageReference = FirebaseStorage.getInstance().getReference("wallpapers");
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
wallpaperList = new ArrayList<>();
storageReference.listAll()
.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(ListResult listResult) {
for (StorageReference item : listResult.getItems()) {
// i want to set-up my Wallpaper object here and add it to my list
// but how can i do that
}
recyclerViewAdapter = new RecyclerViewAdapter(ShowWallpapers.this,wallpaperList);
recyclerView.setadapter(recyclerViewAdapter);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
}
我的存储的控制台视图:https://i.imgur.com/jnjW6Ko.png
StorageReference
中有documentation for how to get the download url。您可能还需要总体上回顾StorageReference文档,因为基本上在列出参考文献时您想要做的所有事情都应该在这里:)
在这种情况下,一旦您进入listAll()
的成功处理程序,就开始启动任务以从存储参考中获取下载URL(或元数据,使用getMetadata()
和类似的方法)。
类似的东西:
storageReference.listAll()
.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(ListResult listResult) {
for (StorageReference item : listResult.getItems()) {
item.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Uri uri) {
// Do whatever you need here.
Log.w(TAG,"downloadUrl:" + uri.toString());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
}
}
});
当然,以上内容在引用位置(通过getPrefixes()
)没有任何前缀,它只是列出了该引用处直接可用的blob。